package greedy;

/**
 * Created with IntelliJ IDEA.
 * Description: 53. 最大子数组和
 * User: Li_yizYa
 * Date: 2025/6/24
 * Time: 21:24
 */
public class Solution3 {
    public int maxSubArray(int[] nums) {
        if (nums == null || nums.length == 0) {
            return Integer.MIN_VALUE;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        int ret = nums[0];
        int sum = nums[0];
        for (int i = 1; i < nums.length; i++) {
            sum += nums[i];
            if (sum < nums[i]) {
                sum = nums[i];
            }
            ret = Math.max(sum, ret);
        }
        return ret;
    }
}
